﻿<UserControl x:Class="IQToolkitCodeGen.View.MappingView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
             xmlns:appCore="clr-namespace:IQToolkitCodeGen.Core"
             mc:Ignorable="d"
             d:DesignHeight="300"
             d:DesignWidth="300">
    <UserControl.Resources>
        <Style TargetType="{x:Type DataGridCell}">
            <Style.Triggers>
                <Trigger Property="IsReadOnly"
                         Value="True">
                    <Setter Property="Background"
                            Value="LightGray" />
                    <Setter Property="Foreground"
                            Value="Black" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>
    <Grid>
        <Border Style="{StaticResource MainContentBorder}">
            <Border Style="{StaticResource MainContentHeader}"
                    VerticalAlignment="Top" />
        </Border>
        <Grid Margin="20">
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <RowDefinition Height="5" />
                <RowDefinition Height="*" />
                <RowDefinition Height="5" />
                <RowDefinition Height="*" />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            <DataGrid x:Name="TableDataGrid"
                      AutoGenerateColumns="False"
                      ItemsSource="{Binding Tables}"
                      SelectedItem="{Binding Path=SelectedTable, Mode=TwoWay}"
                      CanUserAddRows="False">
                <DataGrid.Columns>
                    <DataGridCheckBoxColumn Binding="{Binding Path=Selected}">
                        <DataGridCheckBoxColumn.HeaderTemplate>
                            <DataTemplate>
                                <CheckBox IsChecked="{Binding DataContext.AllTablesSelected, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="Click">
                                            <i:InvokeCommandAction Command="{Binding DataContext.AllTableSelectionCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </CheckBox>
                            </DataTemplate>
                        </DataGridCheckBoxColumn.HeaderTemplate>
                    </DataGridCheckBoxColumn>

                    <DataGridTextColumn Binding="{Binding Path=TableName}"
                                        Header="Table Name"
                                        IsReadOnly="True" />

                    <DataGridTextColumn Binding="{Binding Path=EntityName}"
                                        Header="Entity Name" />

                </DataGrid.Columns>
            </DataGrid>
            <GridSplitter Grid.Row="1"
                          HorizontalAlignment="Stretch"
                          VerticalAlignment="Stretch" />
            <DataGrid x:Name="ColumnDataGrid"
                      Grid.Row="2"
                      AutoGenerateColumns="False"
                      ItemsSource="{Binding Columns}"
                      SelectedItem="{Binding Path=SelectedColumn, Mode=TwoWay}"
                      CanUserAddRows="False">
                <DataGrid.Columns>
                    <DataGridCheckBoxColumn Binding="{Binding Path=Selected}">
                        <DataGridCheckBoxColumn.HeaderTemplate>
                            <DataTemplate>
                                <CheckBox Content="{Binding}"
                                          IsChecked="{Binding DataContext.AllColumnsSelected, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="Click">
                                            <i:InvokeCommandAction Command="{Binding DataContext.AllColumnSelectionCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </CheckBox>
                            </DataTemplate>
                        </DataGridCheckBoxColumn.HeaderTemplate>
                    </DataGridCheckBoxColumn>

                    <DataGridTextColumn Binding="{Binding Path=ColumnName}"
                                        Header="Column Name"
                                        IsReadOnly="True" />

                    <DataGridTextColumn Binding="{Binding Path=PropertyName}"
                                        Header="Property Name" />

                    <DataGridTextColumn Binding="{Binding Path=PropertyType}"
                                        Header="Property Type" />

                    <DataGridCheckBoxColumn Binding="{Binding Path=PrimaryKey}"
                                            Header="Is Primary Key" />

                    <DataGridTextColumn Binding="{Binding Path=DbType}"
                                        Header="DbType" />

                    <DataGridTextColumn Binding="{Binding Path=DefaultValue}"
                                        Header="DefaultValue" />

                    <DataGridCheckBoxColumn Binding="{Binding Path=Generated}"
                                            Header="Is Generated" />

                    <DataGridCheckBoxColumn Binding="{Binding Path=Nullable}"
                                            Header="Is Nullable" />

                    <DataGridTextColumn Binding="{Binding Path=Precision}"
                                        Header="Precision" />

                    <DataGridTextColumn Binding="{Binding Path=Scale}"
                                        Header="Scale" />
                </DataGrid.Columns>
            </DataGrid>
            <GridSplitter Grid.Row="3"
                          HorizontalAlignment="Stretch"
                          VerticalAlignment="Stretch" />
            <DataGrid x:Name="AssociationDataGrid"
                      Grid.Row="4"
                      AutoGenerateColumns="False"
                      ItemsSource="{Binding Associations}"
                      SelectedItem="{Binding Path=SelectedAssociation, Mode=TwoWay}"
                      CanUserAddRows="False">
                <DataGrid.Columns>
                    <DataGridCheckBoxColumn Binding="{Binding Path=Selected}">
                        <DataGridCheckBoxColumn.HeaderTemplate>
                            <DataTemplate>
                                <CheckBox Content="{Binding}"
                                          IsChecked="{Binding DataContext.AllAssociationsSelected, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}">
                                    <i:Interaction.Triggers>
                                        <i:EventTrigger EventName="Click">
                                            <i:InvokeCommandAction Command="{Binding DataContext.AllAssociationSelectionCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" />
                                        </i:EventTrigger>
                                    </i:Interaction.Triggers>
                                </CheckBox>
                            </DataTemplate>
                        </DataGridCheckBoxColumn.HeaderTemplate>

                    </DataGridCheckBoxColumn>

                    <DataGridTextColumn Binding="{Binding Path=AssociationName}"
                                        Header="Association Name"
                                        IsReadOnly="True" />

                    <DataGridTextColumn Binding="{Binding Path=PropertyName}"
                                        Header="Property Name" />

                    <DataGridTextColumn Binding="{Binding Path=KeyMembers}"
                                        Header="Key Memebers"
                                        IsReadOnly="True" />

                    <DataGridTextColumn Binding="{Binding Path=RelatedTableName}"
                                        Header="Related Entity Id"
                                        IsReadOnly="True" />

                    <DataGridTextColumn Binding="{Binding Path=RelatedKeyMembers}"
                                        Header="Related Key Memebers"
                                        IsReadOnly="True" />
                    <DataGridTextColumn Binding="{Binding Path=AssociationType}"
                                        Header="Association Type"
                                        IsReadOnly="True" />
                </DataGrid.Columns>
            </DataGrid>
            <Button Content="Generate Files"
                    Grid.Row="5"
                    Command="{Binding GenerateFilesCommand}"
                    FontWeight="Bold" />
        </Grid>
    </Grid>
</UserControl>
